package com.xsmart.order.dao;

import com.baomidou.mybatisplus.plugins.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public interface RecObjectDao {
    /**
     * 查询
     * @param page
     * @param recObjectMap
     * @return
     */
    @Select("<script>" +
            "select t1.priority_id, t1.seat_id,y1.seat_name, t1.user_id, t3.detail_url, user_name, t1.du_id, obj_id, obj_type, t1.status, obj_title, obj_img," +
            "(CASE WHEN obj_type = '1' THEN y2.goods_name WHEN obj_type = '2' THEN y3.brand_name when obj_type='4' then y4.cat_name END)obj_name, "+
            "date_format(t1.add_time, '%Y-%m-%d %H:%i:%s') as add_time, " +
            "DATE_FORMAT(start_time,'%Y-%m-%d %H:%i:%s') as start_time, " +
            "DATE_FORMAT(end_time,'%Y-%m-%d %H:%i:%s') as end_time " +
            "from yy_priority t1 " +
            "left join yy_priority_info t2 on t1.priority_id = t2.priority_id " +
            "left join yy_detail_url t3 on t1.du_id = t3.du_id " +
            "left join yy_seat y1 on t1.seat_id=y1.seat_id "+
            "left join goods y2 on t1.obj_id=y2.goods_id "+
            "left join brand y3 on t1.obj_id=y3.brand_id "+
            "left join category y4 on t1.obj_id=y4.cat_id "+
            "left join infs_admin_user t4 on t1.user_id = t4.user_id where 1=1 " +
            "<if test = \"seat_id != null\">" +
            "and t1.seat_id=#{seat_id}" +
            "</if>" +
            "<if test = \"du_id != null\">" +
            "and t1.du_id=#{du_id}" +
            "</if>" +
            "</script>")
    List<Map<String, Object>> queryRecObject(Page<Map<String, Object>> page, Map recObjectMap);

    /**
     * 删除推荐对象
     * @param priorityId
     * @return
     */
    @Delete("delete from yy_priority where priority_id=#{priorityId}")
    int delRecObject(Integer priorityId);

    /**
     * 删除推荐对象信息
     * @param priorityId
     * @return
     */
    @Delete("delete from yy_priority_info where priority_id=#{priorityId}")
    int delRecObjectInfo(Integer priorityId);

    /**
     * 更新
     * @param recObjectMap
     * @return
     */
    @Update("update yy_priority set seat_id=#{recObjectMap.seat_id}," +
            "du_id=#{recObjectMap.du_id}," +
            "status=#{recObjectMap.status}," +
            "start_time=#{recObjectMap.start_time}," +
            "end_time=#{recObjectMap.end_time} " +
            "where priority_id =#{recObjectMap.priority_id}")
    int modRecObject(@Param("recObjectMap")Map recObjectMap);

    /**
     * 更新推荐对象信息
     * @param recObjectMap
     * @return
     */
    @Update("update yy_priority_info set obj_title=#{recObjectMap.obj_title}," +
            "obj_img=#{recObjectMap.obj_img} " +
            "where priority_id=#{recObjectMap.priority_id}")
    int modRecObjectInfo(@Param("recObjectMap")Map recObjectMap);

    /**
     * 新增推荐对象
     * @param recObjectMap
     * @return
     */
    @Insert("insert into yy_priority "+
            "(seat_id, "+
            "user_id, " +
            "du_id, "+
            "add_time, "+
            "obj_id, "+
            "obj_type, "+
            "status, "+
            "start_time, "+
            "end_time)"+
            "values (" +
            "#{recObjectMap.seat_id}," +
            "#{recObjectMap.user_id}," +
            "#{recObjectMap.du_id},"+
            "#{recObjectMap.add_time},"+
            "#{recObjectMap.obj_id},"+
            "#{recObjectMap.obj_type},"+
            "#{recObjectMap.status},"+
            "#{recObjectMap.start_time},"+
            "#{recObjectMap.end_time})")
    int addRecObject(@Param("recObjectMap")Map recObjectMap);

    /**
     * 获取新增的推荐对象的ID
     * @return
     */
    @Select("select max(priority_id) as priority_info_id from yy_priority")
    List<Integer> getId();

    /**
     * 新增对象信息
     * @param recObjectMap
     * @return
     */
    @Insert("insert into yy_priority_info "+
            "(priority_id, "+
            "obj_title, "+
            "obj_img)"+
            "values (" +
            "#{recObjectMap.priority_id},"+
            "#{recObjectMap.obj_title},"+
            "#{recObjectMap.obj_img})")
    int addRecObjectInfo(@Param("recObjectMap")Map recObjectMap);

}
